﻿/*
Copyright 2010 Matías Fidemraizer (www.matiasfidemraizer.com).

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

using System;
using System.Collections.Generic;

namespace Comkarl.Architecture.Patterns.Multiton
{
    /// <summary>
    /// Represents the base class for the multiton pattern. 
    /// </summary>
    /// <remarks>
    /// A multiton is a manager class which contains N instances of some type, being the responsible of creating
    /// instances of its type.
    /// </remarks>
    /// <typeparam name="TObjectKey">
    /// The type for the objects' key.
    /// </typeparam>
    /// <typeparam name="TObject">
    /// The type for the managed object.
    /// </typeparam>
    public abstract class Multiton<TObjectKey, TObject>
        where TObjectKey : IEquatable<TObjectKey>
    {
        /// <summary>
        /// Default constructor.
        /// </summary>
        protected Multiton()
        {
        }

        /// <summary>
        /// Static constructor.
        /// </summary>
        static Multiton()
        {
            ValidateObjectType();

            instances = new Dictionary<TObjectKey, TObject>();
        }

        private static Dictionary<TObjectKey, TObject> instances;

        /// <summary>
        /// Gets the collection of managed instances.
        /// </summary>
        private static Dictionary<TObjectKey, TObject> Instances
        {
            get
            {
                lock (instances)
                {
                    return instances;
                }
            }
        }

        /// <summary>
        /// Validates the managed type for the multiton.
        /// </summary>
        private static void ValidateObjectType()
        {
            if (typeof(TObject).GetConstructor(new Type[] { }) == null)
            {
                throw new InvalidMultitonTypeException();
            }
        }

       /// <summary>
       /// Gets a managed instance by this multiton by its key.
       /// </summary>
       /// <param name="key">
       /// The key for the object.
       /// </param>
       /// <returns>
       /// The object.
       /// </returns>
        public static TObject GetInstance(TObjectKey key)
        {
            if (Instances.ContainsKey(key))
            {
                return Instances[key];
            }
            else
            {
                TObject newInstance = (TObject)typeof(TObject).GetConstructor(new Type[] { }).Invoke(null);
                Instances.Add(key, newInstance);

                return newInstance;
            }
        }
    }
}